from flask import Flask
from flask_cors import CORS
from .extensions import db
from .dbmodel import *
from app.cli_tool import initdb
import os

def create_app():
    app = Flask(__name__, instance_relative_config=True)
    CORS(app)

    # 加载配置：instance目录下的config.py文件
    config_file = os.path.join(app.instance_path, 'config.py')
    app.config.from_pyfile(config_file)
    app.logger.info(app.config)
    app.logger.info(app.instance_path)

    @app.route('/')
    def index():
        return 'Hello'

    # 加载Flask-SQLAlchemy
    db.init_app(app)

    # 注册命令
    app.cli.add_command(initdb)

    # 注册蓝图
    from .blueprints import api_bp, user_bp
    app.register_blueprint(api_bp, url_prefix='/api')
    app.register_blueprint(user_bp, url_prefix='/user')
    

    return app